package com.ujoku.view.builder;

import com.ujoku.domain.Address;
import com.ujoku.request.OrderForm;
import com.ujoku.service.AddressService;
import com.ujoku.service.RegionService;
import com.ujoku.service.ShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

/**
 * Created by Andy.Lian on 2014/11/6.
 */
@Component
public class OrderViewBuilder {

    @Autowired
    protected RegionService regionService;
    @Autowired
    protected AddressService addressService;
    @Autowired
    protected ShippingService shippingService;

    private static Pattern pattern=Pattern.compile("^1[3-8]{1}\\d{9}$");

    public Address BuildAddress(OrderForm form){
        Address address=null;
/*        if(form.getAddressId()>0){
            address= (Address) addressService.selectById(String.valueOf(form.getAddressId()));
        }else if (!form.getConsignee().isEmpty() && !form.getAddress().isEmpty() && !form.getPhone_mob().isEmpty()){
            if(!pattern.matcher(form.getPhone_mob()).matches()){
                throw new InvalidRequestException("请输入正确的手机号码！");
            }
            address=new Address();
            address.setAddress(form.getAddress());
            address.setConsignee(form.getConsignee());
            address.setPhone_mob(form.getPhone_mob());
            Region region= (Region) regionService.selectById(form.getRegion_id());
            if(region!=null){
                address.setRegion_id(region.getRegion_id());
                address.setRegion_name(region.getRegion_name());
            }
            if(form.getAutoSaveAddress()){
                addressService.insert(address);
            }
        }*/

        return address;
    }

/*    public Order BuildOrder(Member member, List<Cart>carts){
        float goods_amount=0;
        for(int i=0;i<carts.size();i++){
            //goods_amount+=carts.get(i).getQuantity() * carts.get(i).getPrice();
        }
        Order order=new Order();
        order.setBuyer_id(member.getUser_id());
        order.setBuyer_name(member.getUser_name());
        order.setBuyer_email(member.getEmail());
        order.setSeller_id(1);
        order.setSeller_name("优酒库");
        order.setGoods_amount(goods_amount);
        order.setOrder_amount(goods_amount);
        return order;
    }*/

 /*   public OrderExtm BuildOrderExtm(Order order, Address address, int shippingId){
        OrderExtm orderExtm=new OrderExtm();
        orderExtm.setOrder_id(order.getOrder_id());
        orderExtm.setConsignee(address.getConsignee());
        orderExtm.setRegion_id(address.getRegion_id());
        orderExtm.setRegion_name(address.getRegion_name());
        orderExtm.setAddress(address.getAddress());
        orderExtm.setZipCode(address.getZipcode());
        orderExtm.setPhone_mob(address.getPhone_mob());
        orderExtm.setShipping_id(shippingId);
        Shipping shipping= (Shipping) shippingService.selectById(shippingId);
        orderExtm.setShipping_name(shipping.getShipping_name());
        return orderExtm;
    }*/

/*    public OrderView BuildOrderView(Order order)
    {
        OrderView orderView=new OrderView();
        orderView.setOrder_id(order.getOrder_id());
        orderView.setStatus(order.getStatus());
        orderView.setOrder_sn(order.getOrder_sn());
        orderView.setAdd_time(order.getAdd_time());
        orderView.setGoods_amount(order.getGoods_amount());
        orderView.setOrder_amount(order.getOrder_amount());
        return orderView;
    }

    public OrderGoodsView BuildOrderGoodsView(OrderGoods goods){
        OrderGoodsView orderGoodsView=new OrderGoodsView();
        orderGoodsView.setOrder_id(goods.getOrder_id());
        orderGoodsView.setGoods_id(goods.getGoods_id());
        orderGoodsView.setGoods_name(goods.getGoods_name());
        orderGoodsView.setGoods_image(goods.getGoods_image());
        orderGoodsView.setQuantity(goods.getQuantity());
        orderGoodsView.setPrice(goods.getPrice());
        return orderGoodsView;
    }*/
}
